package com.cdacp.openapi.example;

import com.alibaba.fastjson.JSON;
import com.cdacp.openapi.common.Signature;
import com.cdacp.openapi.test.TestBase;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 查询回调历史接口
 */
public class NotifyHistoryTest extends TestBase {

    String url = "http://localhost:8081";
    String appId = "20201224791691613381328896";
    // 平台提供的私钥
    String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCejscW/1fRqcuhIeyEowftpMkvb6eCjvjCTTJWBsMi6JuEx5zXvRIIrNylY2cxQ5T7svP5boA9Y4AjVfU5nmH+7m3Bz2+3FgJKpjiKsi8kjvJE5trm6uleZheGBo1ooR+vpmcxgZ5fpJrts3WLodes9+xpH10wSTPeLwpaxM5+TprZGbsqD2ONdtgjun0zIXzZWHevZmTMcn6CiB20IYCR7C8OGp6b0W551DSUE0zK8D4F2+ny9zaoR2kSHKCurSNsfxeIR+NOpCkjLZY6loCVcOvmoqxhcK6jxvDxdL7tV0OfZNcXMLm0DnRB4VhbIZRo32nGFcZ20A0Cq8tcNtpzAgMBAAECggEAHbjL7y6sSpwWlLzkNjTNXqm+IMtATkP4s4t+/QtFdvy9clVsvDAv+jpVHheVF3IAsnHoNACXv2owce9CCav72KrfHi6BVYOlqFKiVLV0CtlU5r+Tlye/2Tcv/6qoxU02wFlkDK6sfmc/t4ElbqhG4qCSy13avRtrwqF0wnET+C79k8bqDfQmTCO9PIwdXM04zbgydBKHonB/TP4H2SZ1qBiWs22PSgMj3KK0MN3VLJ3Y4xytxGs5fKS+DjtlU/ZWL98owQ5zrr3Pdhp/galoolqXSykGcoQs6n+W980psJ+qdhu1n82GO8ioB9Sj/pNmdW8Dh6IMfItb8pzeOsUAOQKBgQDpZfjv9E2Zeb719igN7Ttt1PvkTgeQ+aaKIvE52HbmiAKqtc9b32mDhepTqzgHo6HQQDRoQbd9GWCMkVIoY/gA04LV0JvtxO9r05Zd+CD0J6iM8EqbRQ4uilL4NKUsC0blCy8nmSNpn7IPg1ivfS7A0S4ezW3omA8e2Tgl69tpNwKBgQCt6XrnEt6FMzRSnbznvd/GEB1OgPY5g/uovZWp9xVXEsNttspxhvJJmwajofwMUVub6MFZSN6T/UHedmIUHTQUZvyeZvsmi7znaAOhHX5bZqa5Z/14NUYRWZqd/7hkJZ1cRUUsjc2BOx9nQW1CwRRAKgfrPzkw+3OMCM1BNrFGpQKBgQCJTLSBrAzOQUFl/dWYQ7tXJBi+OGzZCxO+ZUnUxpNDkDVJaUS/fhDbDzdG2w1N6SOyl2J9BlPDot8aMg7LtetZyRNJZrujxts1ajYlt2bjWkUcC+tHpfruw+6FFqsn2sZszQSDXeHoK8qat6C/M3wCb8ua0SxbzL/wXMZcAICFRwKBgF5S6NbVNB04ZWN9rAxgvjK1YL/IUENnoyGlGbxih49/XMFvx6MFVvm2/EXZwFOJ+M650cw0KoxHaudWe+F+6VU7ej4LiqIgXieAEZ/b2EGIjlq23RHKiyMz3xlcrcybXhrCcz8z9cbmoySkprby86HNahgcZU98wfGsgG6jVEkFAoGBALsY0/y9Q4ET8Mvv/gv2/kSMAtec0oDDoamY7OKqvqLqDhr91A0AjRD0+SU+GS9K6IVCMmV6Hv9Yz3+v/kfyL/gHlOFSsVWvJhMUPCnLWqyqHtzFqM6+2g495EhTBLpyzPN0P+I1G/nI60gccs05Humn7jsMwr8s2dzDP0g2F3tD";


    /**
     参数	            类型	    是否必填	    最大长度	    描述	            示例值
     app_id	        String	是	        32	    cdacp分配给开发者的应用ID	2014072300007148
     method	        String	是	        128	    接口名称	trade.fastpay.refund.query
     format	        String	否	        40	    仅支持JSON	JSON
     charset	        String	是	        10	    请求使用的编码格式，如utf-8,gbk,gb2312等	utf-8
     sign_type	    String	是	        10	    商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2	RSA2
     sign	        String	是	        344	    商户请求参数的签名串，详见签名	详见示例
     timestamp	    String	是	        19	    发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"	2014-07-24 03:07:50
     version	        String	是	        3	    调用的接口版本，固定为：1.0	1.0
     app_auth_token	String	否	        40	    详见应用授权概述
     biz_content	    String	是		请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档
     */
    // 这个请求会路由到服务
    @Test
    public void testGet() throws Exception {

        // 公共请求参数
        Map<String, String> params = new HashMap<String, String>();
        params.put("app_id", appId);
        params.put("method", "history.notify.param.post");//方法名
        params.put("format", "json");
        params.put("charset", "utf-8");
        params.put("sign_type", "RSA2");
        params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        params.put("version", "1.0");

        // 业务参数
        Map<String, Object> bizContent = new HashMap<>();
        bizContent.put("packageName", "com.acp");
        bizContent.put("pageNum", 1);//app接口支付必传
        bizContent.put("pageSize",3);
        bizContent.put("appId", appId);
        params.put("biz_content", JSON.toJSONString(bizContent));
        String content = Signature.getSignContent(params);
        String sign = Signature.rsa256Sign(content, privateKey, "utf-8");
        params.put("sign", sign);

        System.out.println("----------- 请求信息 -----------");
        System.out.println("请求参数：" + buildParamQuery(params));
        System.out.println("商户秘钥：" + privateKey);
        System.out.println("待签名内容：" + content);
        System.out.println("签名(sign)：" + sign);
        System.out.println("URL参数：" + buildUrlQuery(params));

        System.out.println("----------- 返回结果 -----------");
        String responseData = post(url, params);// 发送请求
        System.out.println(responseData);
    }


}
